import { verifyAccessToken } from '~/utils/jwt-utils';
import { unAuthorizedResponse, useResponseSuccess, useResponseError } from '~/utils/response';
import { queryDatabase } from '~/functions';

export default eventHandler(async (event) => {
  const userinfo = verifyAccessToken(event);
  if (!userinfo) {
    return unAuthorizedResponse(event);
  }
  try {
    // 读取请求体中的数据
    const data = await readBody(event);
    const values = data.map((item) => item.value);

    if (values.length === 0) {
      return useResponseError('请求数据中没有有效的值');
    }
    // 查询数据库中name字段为value的记录
    const queryString = 'SELECT * FROM component WHERE name IN (?) ORDER BY name ASC';
    const results = <any[]>await queryDatabase(queryString, [values]);

    // 返回查询结果
    if (results.length > 0) {
      return useResponseSuccess(results);
    } else {
      return useResponseError('未找到匹配的记录');
    }
  } catch (error) {
    return useResponseError(error.message);
  }
});
